<?php

    // +----------------------------------------------------------------------
    // | 行为日志控制器
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\ucenter\admin;

    use app\model\ucenter\ActionLog as ActionLogModel;
    use app\model\ucenter\Action as ActionModel;
    use app\event\ucenter\Action as ActionEvent;
    use app\event\ucenter\User as UserEvent;
	use app\controller\admin\Common;
    use think\Exception;
    use think\facade\Config;
    use yicmf\builder\Table;
    use yicmf\builder\View;
    use think\facade\Url;

    class ActionLog extends Common
    {
        protected function initialize()
        {
            parent::initialize();
            $this->event = new ActionEvent();
        }


        public function _filter()
        {
            $map[] = ['status', '>', -2];
//            if (!UserEvent::isAdministrator($this->user)) {
//                $map[] = ['user_id', '=', $this->user['id']];
//            }else{
                $ids = \app\model\ucenter\User::where('store_id',$this->store['id'])->where('type','<>',99)->column('id');
            $map[] = ['user_id', 'in', $ids];
//            }
            return $map;
        }


        /**
         * @return mixed|string
         * @author  : 微尘 <yicmf@qq.com>
         */
        public function index()
        {
            $statusOptions = \app\model\ucenter\User::where('store_id',$this->store['id'])->where('type',1)->field('id,nickname as value')->select();
            $tags = ActionModel::where('status',1)->select();
            // 显示页面
            $builder = new Table();
            $builder->model(ActionLogModel::class)
                ->tag($tags,'日志名称','','action_id')
                ->where($this->_filter())
                ->order('id DESC')
				->searchTextLike('message', '记录信息', '')
                //                ->suggest('请定期清理日志，提示网站运行性能')
                ->searchSelect('user_id','指定客服',$statusOptions)
//                ->searchDateRange('create_time', '时间：','111','-1 day')
//                ->buttonDeleteAll('clear', '清除'.config('setting.log_max_time').'天前日志')
                ->keyId()
                ->keyText('action.title', '日志名称', false, 120)
                ->keyUser('user_id', '执行者')
                ->keyText('message', '记录信息')
                ->keyIp()
                ->keyCreateTime()
                ->actionView()
//                ->actionDelete()
            ;
            return $builder->fetch();
        }

        /**
         * 浏览日志详情.
         */
        public function view()
        {
            $data = ActionLogModel::where('id', $this->request->param('id'))->where('status', 1)->find();
            if ($data) {
                $builder = new View();
                $builder->title('查看日志信息')
                    ->keyLabel('action.title', '操作类型')
                    ->keyLabel('message', '记录信息')
                    ->keyLabel('os', '运行环境')
                    ->keyLabel('url', 'url')
                    ->keyLabel('create_time', '操作时间')
                    ->data($data);
                return $builder->fetch();
            } else {
                $this->_returnError( '详情查询失败，该日志可能已经被删除', true);
            }
        }

		public function delete()
		{
			$course = ActionLogModel::where('id', $this->request->param('id'))->find();
			if ($course) {
				$course['status'] = -2;
				if ($course->save()) {
					$this->_returnSuccess('删除成功');
				} else {
					$this->_returnError('删除失败');
				}
			} else {
				$this->_returnError('操作异常');
			}
		}
		public function clear()
		{
			$event = new ActionEvent();
			return $this->_returnAuto($event->clear());
		}
    }
